Archived topic
Custom hook content with input field directly in page editing
14 replies · Started by René on May 26, 2022
Hello,
I would like to create several subpages where a different table & text should be displayed at the top according to the post. The table & the text should cover the full width of the page and then the sidebar and the rest of the content begins below.
Of course, I could create a separate hook element for each subpage, but that would be far too time-consuming, because well over 100 subpages are to be created in this layout.
Here is a graphic that shows how the layout should look like: https://ibb.co/VHXz73T
Is there a better way to do this in GP? Thanks very much
Hi there,
how and where is that table and text saved on your site ?
Hey David,
sorry for my late aswer. It is the AAWP (Plugin) Table and i insert it via a Custom Field. The text should be taken from the normal post editor. As I said, several posts should be created and I don't want to have to create a hook for each post. Here is an example: https://www.vergleich.org/arbeitssocken/ The sidebar starts below the table.
Sorry for my bad english.
and i insert it via a Custom Field
What exactly is it you insert in the custom field? Is it a shortcode ?
Yes, the Table is a Shortcode from AAWP.
You could do something like this:
add_action('generate_after_entry_header', function(){
$aawpfield = get_post_meta( get_the_id(), 'your_custom_field_name', true );
if ( $aawpfield && is_single() ) {
echo do_shortcode( $aawpfield );
}
});
Just update the your_custom_field_name to match your custom field.
If you want to do it in a GP HooK Element you only need:
<?php
$aawpfield = get_post_meta( get_the_id(), 'your_custom_field_name', true );
if ( $aawpfield && is_single() ) {
echo do_shortcode( $aawpfield );
}
?>
Thank you for your prompt reply. Then that would be clarified with the table :) But how do I do that with the text?
Is the text also dynamic ? Where does that come from ?
as previously mentioned, the text should be taken from the normal editor. It is enough if the first paragraph is taken. I have already written a PHP script that reads the text of the first paragraph and displays it at the top. Unfortunately, the paragraph is then displayed twice and I have to hide one with CSS. But it's not a nice solution.
if there is no good solution for this in the theme, maybe you could put the content in the editor to be displayed above in a shortcode. But my programming skills are not enough for that.
We don't interfere with the Editor, there are some options:
1, Use the manual Excerpt field to add your text.
2. Use a custom field to store the text. I would recommend using ACF for that Custom Field as it will allow you to store Rich Text
Then I'll probably stay with my PHP script, which reads the first paragraph and places it above (copies). But maybe you have a script for me that removes the copied paragraph (the original)? So far I only hide the second paragraph with CSS. I don't know how Google interprets that
Can you share the code you're using to extract the first paragraph? As you should be able to include a string replace to remove it too.
A programmer friend of mine (https://github.com/Lightweb-Media) has now helped me out. Here's the code
Functions.php:
function get_first_para($content){
$output = preg_match_all('%(<p[^>]*>.*?</p>)%i', $content, $matches);
$first_para = $matches [1] [0];
return $first_para;
}
function remove_first_para($content, $first_para){
return str_replace($first_para, "",$content);
}
add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 10,1);
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single Post.
if ( is_singular() && in_the_loop() && is_main_query() ) {
$first_para = get_first_para($content);
$content = remove_first_para($content, $first_para);
return $content;
}
return $content;
}
Header.php:
do_action( 'generate_after_header' );
?>
<div <?php generate_do_attr( 'page' ); ?>>
<div style="padding: 20px 10px 0px 10px;">
<?php
/**
* generate_inside_site_container hook.
*
* @since 2.4
*/
if (is_page_template('page-wide-table.php')) {
the_title( '<h1>', ': Die besten Produkte im Vergleich ('. do_shortcode("[aktuelles_jahr]").')</h1>');
$first_para = get_first_para(get_the_content());
$first_para = str_replace("[aktuelles_jahr]", do_shortcode("[aktuelles_jahr]") , $first_para);
echo '<p>'.$first_para.'</p>';
echo do_shortcode(get_post_meta($post->ID, 'cf-wide-table', true));
}
?>
</div>
<?php
do_action( 'generate_inside_site_container' );
?>
Now it works, fine. Still not the best solution, but ok.
Glad to hear you got a solution. And thanks for sharing that.