Wordpress Display Child page data: Title, featured image and custom field - wordpress

I'm displaying the child pages of the current page. With the featured image, title pulling through. I also want to pull through a custom field I've added. I'm outputting this at html so it looks like each child page has its own box, with the featured image filling the box, the title on top of the image and the the custom field value sitting under the title.
But I can't get the custom field to display properly, I just get the value 1, not sure if I've converted the array properly?
Could you help me out to output the custom field 'PageTag'
https://i.stack.imgur.com/vzCBU.png
https://i.stack.imgur.com/BwfuV.png
Wordpress template code
<div class="childPages">
<?php
$subs = new WP_Query(
array(
'post_parent' => $post->ID,
'post_type' => 'page',
'meta_key' => '_thumbnail_id'
)
);
if( $subs->have_posts() ) :
while( $subs->have_posts() ) :
$subs->the_post();
$pageTag = get_post_meta($post->ID, '_PageTag' , true);
echo '<div class="childPageBlock"> '.get_the_post_thumbnail().'<div class="childPageBlockText"><h3>'.get_the_title().'</h3><span>'.print_r( $pageTag ).'</span></div></div>';
endwhile;
endif;
wp_reset_postdata(); ?>
</div>

Removing the print_r() and leaving its as
'.$pageTag.'
Worked!

Related

Remove images from content in wordpress gallery template

I want to add a gallery template to a child theme and am finding it frustratingly difficult to find good examples. The best example I've found to date is by Andres Hermosilla, but was made way back in 2012.
Steps 1 to 3 will get me half of what I want, which is a gallery inserted above the site title (I don't need fluxslider or similar at this stage.) The following is the code I'm using, placed just before the_title():
<?php
$image_args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order'=> 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment'
);
$images = get_children( $image_args );
if ( $images ) {
foreach ( $images as $image ) { ?>
<img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
<?php }
} ?>
My issue is that the images themselves remain displayed in the content of the post - how can I strip all of the images from the page content at the same time?
Why not add a class to the gallery template page and display none all the images except for gallery ones using css.

WordPress wp_query + filter result (no custom filed) + next_post_link()

I did an extensive search but could not find the answer (perhaps I'm not using the right search terms?).
Anyway, here's what I'm trying to accomplish:
I'm using wp_query to return user submitted posts that have been published. I am showing one post at a time and using next_posts_link() to let the user to advance to the next post. This is all working fine. I'm also using the wp-postratings plugin to the user to rate these user submissions. I only want to show posts that the user has not already rated. For that, I'm using:
check_rated_username($post->ID)
This part is actually also working. So far my code looks like this:
$my_query = new WP_Query( array (
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
));
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
$rated = check_rated_username($post->ID);
if (!$rated) : ?>
<?php //the_title() etc. Show post details (it's long so I'm not going to post the whole thing, but the post details are showing up fine) ?>
<?php next_posts_link('Next →', 10); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif; ?>
The problem next_posts_link() retrieves posts that meet the parameters set in the wp_query (and therefore are not part of the "if (!rated)" statement. There are instances when I click on "Next", and it shows a blank page--a post that this particular user has already rated.
How can I set it up so that I'm showing one post that the user has not rated on each page AND allow the user to navigate to the next unrated post by clicking on a NEXT button? I'm not married to the approach I have come up with. I just need to achieve this end result.
Thanks!
What you can do is first to fetch all posts id:s that a user has rate, then you can add an post__not_in in your query. It can look something like this:
<?php
/* Get all posts id:s that current user has rated */
$user_ratings = $wpdb->get_results( $wpdb->prepare( "SELECT rating_postid FROM {$wpdb->ratings} WHERE rating_userid = %d", get_current_user_id() ) ); ?>
<?php
/*Set up an empty array for save id:s in */
$post_not_in = array();
/* Check if $userRating has any values, if so loop thru and put the id into the array */
if($user_ratings){
foreach($user_ratings as $user_rating){
$post_not_in[] = $user_rating->rating_postid;
}
}
?>
<?php
/* The loop */
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'post__not_in' => $post_not_in
);
$my_query = new WP_Query($args);?>
The rest of your code
This way the loop only have posts that the current user haven't rated yet.

Wordpress custom taxonomy logic

I'm sitting here with an issue to figure out. I'll explain it shortly:
On the homepage of a webiste i am developing , i have a few tiles with brands which link to a custom taxonomy ( with some products arranged by current brand, some info , and a slider image). The slider image is the issue. Becouse this is a custom taxonomy it generates each brands page. So when it is trying to the_field('header-image') inside the loop, it returns with all images , even if i add a posts_per_page = 1 to the wp_query $args. Then it will display the first image to all brands.
<div class="image-container">
<?php
$args = array(
'post_type' => 'brands',
'posts_per_page' => 1,
'order' => 'ASC'
);
$myPosts = new WP_query($args);
if ($myPosts->have_posts()) : while ($myPosts->have_posts()) : $myPosts->the_post();
?>
<img src="<?php the_field("header-image"); ?>" alt="">
<?php
endwhile; endif;
wp_reset_query();
?>
</div>
Does anyone have a clue ?
Thanks in advance,
Dimotro
I think you need to get deep understanding about post_type, taxonomy and terms or may you can check the acf documentation about displaying that fields here

Wordpress - Featured image on a custom post archive page

i have created a custom post named Products.
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' )
);
I have also created a php file named archive-products.php and made it into a template.
In Wordpress I have created a page named Products and selected the products template.
On that static page (that uses the archive template) I have uploaded a image into the Featured Image panel.
In my header I have the code:
echo get_the_post_thumbnail();
But this echos the Featured image of the last custom post in the list (all the products posts have a featured image as well), not the Featured image of the static/archive page, which is what i want. Is this possible to achieve?
Thanks!
I did the very same thing and came across the following answer that solved my problem: https://wordpress.stackexchange.com/a/175228
Save your custom post type archive template as a page.
For example, page-products.php
Backup locally and delete your custom post type archive template from your server.
Show the image with the_post_thumbnail(), put it in a variable with get_the_post_thumbnail(), or make it a background image with your page title over it:
$bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
if( is_page('products') ) : ?>
<div style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;">
<?php the_title( '<h1 class="page-title">', '</h1>' ); ?>
</div>
<?php endif; ?>
Save your permalinks and refresh your page.
This is what worked for me. Hope it helps somebody. :)

Display the content and title of a specific page on Homepage

I want the title and content of a specific page to show in my Homepage and footer area.
It will be something like this.
This is the Title of the page
This is the content of the page, etc....blahh blah..
readmore...
I know that there is no excerpt in the page post. But i want to limit the characters of the content that will show in the footer area.
Can you give me a code/link/tutorial that will show me how to do this?
Thanks,
First there is excerpt avaIlable in the page posts. Just set it in the Screen Options.
About the code try this one which I use on my projects.
It will show excerpt of those pages, which have custom field named showOnHomePage and the value of this field determines the order in the list.
$pages=get_posts( array( 'meta_key' => 'showOnHomePage', 'post_type' => 'page', 'orderby' => 'meta_value', 'order' => 'ASC'));
foreach( $pages as $page )
{
echo '<h2 class="entry-title"><a href="'.get_permalink( $page->ID ).'" >'.get_the_title( $page->ID ).'</a></h2>';
echo $page->post_excerpt;
echo '<a class="read-more" href="'. get_permalink( $page->ID ) . '">Read More</a>';
}

Resources