Wordpress post_title() notice - php

here is my scenario: I got an index.php which loads every page I got one below the other. It's a one-pager in the end.
I found this snippet online and It helped me a lot by creating this.
<?php
$mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<section>
<div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $page->post_title; ?></div>
<div class="page-content"><?php echo $content; ?></div>
</section>
<?php
}
?>
It seems to work just fine. But now to the problem part of this. I want to display the posts also on this one page. My try was to create another page news and place a widget on this page which shows the latests posts, filtered by category.
But now the page title won't show up anymore, all I got is the following
notice: NOTICE: TRYING TO GET PROPERTY 'POST_TITLE' OF NON-OBJECT IN
So I thought the problem might have to do with the little <?php echo $page->post_title; ?> I already tried to just get the_titlebut I ended up getting the same error.
I'm kinda new to Wordpress so I don't know how to deal with this.
Is there another way to get the page title on my index.php?
Best,
Dominik

I managed to fix this. As #delboy1978uk told me, I check if the title is not empty by adding 2 lines of code.
Here is the final code:
<?php
$mypages = get_pages( array('child_of' => 1503, 'sort_column' => 'menu_order') );
foreach( $mypages as $page ) {
$content = $page->post_content;
$title = $page->post_title;
if ( ! $content ) // Check for empty page
if ( ! $title) // Check for empty title < NEW!
continue;
$content = apply_filters( 'the_content', $content );
?>
<section>
<div class="headlines info-header"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow.png" alt="arrow"><?php echo $title; ?></div>
<div class="page-content"><?php echo $content; ?></div>
</section>
<?php
}
?>

Related

WordPress wp_list_pages - Change children of parent from URLS into #anchor links

I'm setting up a side navigation menu using wp_list_pages and I would like to convert the children links from URLs into anchor links i.e #link rather than the current https://example.com/link being generated by wp_list_pages .
I was attempting to use the following code:
<?php
$my_pages = wp_list_pages('echo=0&title_li=&child_of=5&depth=1');
$pieces = explode('"', $my_pages);
$i = 5;
$j = 3;
$limit = count($pieces);
for (;$i<$limit;) {
$tmp1 = '#'.$pieces[$j];
$pieces[$i] = $tmp1;
$i = $i+6;
$j = $j+6;
}
$tmp2 = implode('"',$pieces);
echo $tmp2;
?>
But it seems to be very old and I can't wrap my head around how to properly implement it into my current structure. Maybe this code is useless for what I'm trying to do but I couldn't find anything that would work.
This is what I have currently:
<div class="hero-container">
<?php
global $children;
global $post;
if ( $post->post_parent ) {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->post_parent,
'echo' => 0
) );
} else {
$children = wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'echo' => 0
) );
}
if ( $children ) : ?>
<?php echo '<div class="hero-side-menu">', '<h1>', get_the_title(), '</h1>', '<ul>', $children, '</ul>', '</div>' ?>
<?php endif; ?>
</div>
Any suggestions would be very much appreciated as I've been trying to figure this out for a few days and have gotten nowhere... also would appreciate an explanation as I'm trying to learn where I went wrong!
I need to modify the children of the parent pages to have #anchor links rather than URLS as I've condensed the pages into their parents but still wish to have them as options within the side menu.
Clarification: I have a page with children pages that I referred to as parents however, they are indeed children. I would like to keep the URLS for the children pages and then make the children of the children #links.
This depends a bit on what you want the anchor text to be. Whether it's an ID or a title, etc. But you can modify that to your need.
You may want to try get_pages() instead, so you have a little more control over the output. Something like the following:
<div class="hero-container">
<?php
global $post;
$current_page_title = $post->post_title; // current page title
$child_pages =
get_pages(
array(
'child_of' => $post->ID
),
);
if ($child_pages) : ?>
<div class="hero-side-menu">
<h1><?php echo $current_page_title; ?></h1>
<ul>
<?php foreach ($child_pages as $page) : ?>
<li><?php echo $page->post_title; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>

How can I turn this PHP snippet into a Wordpress shortcode?

This is a music website with multiple artist pages - one page per artist. New content is added as a post with a Wordpress tag to denote the artist. This is so that I can add a Wordpress loop on each artist page to show all the posts filtered with that artist's tag.
I've got the filtered loop working correctly, but unfortunately it's currently hardwritten inside the page template's HTML, so it's only filtering for one tag. I don't want to create a new page template for each artist, so I'd like to add it to my functions.php file instead, where I can instead create a new shortcode for each artist.
Here's the current code in my page template, which filters the loop for all posts with our seefour tag:
<?php
query_posts( "tag=seefour" );
if ( have_posts() ) { ?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
I'm assuming the best option is to turn this into a seefour shortcode inside my functions.php file - how can I achieve this?
Bonus question: is this sustainable in the long run (with 30-50+ artists) or will it cause a lot of redundant code? Open to suggestions...
P.S. I know this kind of question has been answered already (starting with raw PHP), but since I'm starting with a mix of HTML/PHP (and I'm a PHP newb), I just can't get it to work. So my apologies for asking again.
First of all, you should never ever use query_posts(). It is internal WordPress function to create and maintain the main WordPress cycle. Using it, you can crash your site in unpredictable manner. You should use get_posts() or WP_Query instead.
To have your custom shortcode, add the following to your functions.php:
function showtag_shortcode( $atts ) {
$atts = shortcode_atts( array(
'tag' => '', // Default value.
), $atts );
$posts = get_posts( 'tag=' . $atts['tag'] );
if ( $posts ) {
$output = '';
foreach ( $posts as $post ) {
setup_postdata( $post );
$output .= '<div class="jd-box">';
$output .= '<a href="' . get_the_permalink( $post ) . '">';
$output .= get_the_post_thumbnail( $post );
$output .= '<div class="jd-overlay"></div>';
$output .= '<div class="jd-overlay-text">';
$output .= get_the_title( $post );
$output .= '</div>';
$output .= '</a>';
$output .= '</div>';
}
} else {
$output = 'no data';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'showtag', 'showtag_shortcode' );
This function created [showtag] shortcode with one parameter: tag. You can use this shortcode on any page as follows:
[showtag tag="seefour"]
[showtag tag="disco"]
etc. You will have posts with relevant tags to be shown in place of your shortcode.
Putting a whole loop in a shortcode will make it messy, I know you can use shortcodes in Widgets etc as well but I guess that's not what you're after.
If so, the best option would be to make this code a page template say Artist and pass a variable in URL i.e. http://example.com/artist?t=seefour and then use the following code in the page template.
<?php
/**
* Template Name: Artist
*/
query_posts( "tag=" . $_GET['t'] );
if ( have_posts() ) {
?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php
}
}
}
?>
This can be used for any number of artists, totally flexible because it is dependant on a variable that is provided on run time (when accessed).

how to define count++ in wordpress php?

i built a short-code in wordpress to show related posts in the sidebar. Please look on the right at this website: http://www.immvestwolf.de/news/
But i get a php error (Notice: Undefined variable: count in /web/1/000/045/787/175759/htdocs/immvestwolf/wp-content/themes/le-quartier/functions.php on line 74)
i dont knwo whats wrong with this code. On other sites the code works without an php error.
Here is my php code :
function my_recent_posts_with_image() {
// Lese die letzten zehn publizierten Artikel aus
$args = array(
'posts_per_page' => 10
);
$recent_posts = get_posts( $args );
echo '<div class="widget recent_posts_with_image_by_jongo">';
echo '<h5 class="widget_title">Die 10 letzten News</h5>';
foreach ( $recent_posts as $post ) {
$count++;
?>
<div>
<a title="Veröffentlich am: <?php echo get_the_time('d.m.Y', $post->ID ) ?>" href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, array(70,50) ); ?><p><?php echo get_the_title( $post->ID ); ?></p>
</a>
</div>
<?php
}
echo '</div>';
}
add_shortcode('get_recent_posts_with_image','my_recent_posts_with_image');
The error is at the line $count++;
Any ideas to correct this?
$count is never defined...It's up to you to define it. For example:
$count = 0;
foreach ( $recent_posts as $post ) {
That said, you're never even using the value of $count...so it appears as if you could remove it altogether.
Another thing to note is that if you were to use a WP_Query instead of get_posts(), you would have access to a $current_post property for this purpose, without having to manually set up a counter.

Exclude excerpt from wordpress wp_query

Hellow everyone!
I am showing blog of posts in additional wp template and everything works fine, but I've made some kind of gallery from it, and I don't need to show anything except gallery and title.
Inside posts I have this:
[gallery ids="1618,...,1634"]
<h2>...</h2>
<p>...</p>
text without format, etc.
As you can see, I am using a gallery shortcode. I need it to be shown, but all other content to be excluded from the loop.
Really appreciate your help in this question...
My template code:
<?php
/*
* Template name: Блог
*/
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $current_page,
'cat' => 8
);
query_posts( $args );
$wp_query->is_archive = true;
$wp_query->is_home = false;
while(have_posts()): the_post();
?>
<div class="foto_posts">
<?php the_content() ?>
<?php echo '' . get_the_title() . '';?>
</div>
<?php
endwhile;
if(function_exists('page_navi_slider')) page_navi_slider();
try to replace <?php the_content() ?> to <?php the_title() ?>
if you just want to show gallery shortcode try this
replace
<?php the_content() ?>
to
$pattern = get_shortcode_regex();
preg_match('/'.$pattern.'/s', $post->post_content, $matches);
if (is_array($matches) && $matches[2] == 'gallery') {
$shortcode = $matches[0];
echo do_shortcode($shortcode);
}
Try this for getting galleries and videos use the get_post_meta_key()
<?php get_post_meta($post_id,'key_name',1);?>
If it returns an array then traverse that array to get the images link and video links
add_shortcode('gallery', 'gallery_shortcode_fancybox');
function gallery_shortcode_fancybox($attr) {
$attachment_ids = explode(',',$attr['ids']);
$args = array(
'post__in' => $attachment_ids,
//'cat' => 8 if category needs to be shown pass it in shortcode , same as ids
);
$gallery_posts = query_posts( $args );
foreach ($gallery_posts as $key => $value) {
//do the stuff here
echo '<h2>'. $value->post_title;'</h2>';
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($value->ID);
echo '<p><img src="'.$feat_image.'" ></p>';
}
}

Simple Wordpress Loop result completely wrong and full of breaks and <p> elements

I am trying to make a simple loop through my wordpress posts but when it comes to output the whole thing the loop puts it anywhere but not where I said, it should be. For example the loop adds breaks and but they are not in my posts. I had this problem once but dont know anymore what i have done to get over this problem.
MY LOOP:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$post_id = get_the_ID();
$content = the_content();
$headline = get_post_meta($post_id, 'headline', true);
$subtitle = get_post_meta($post_id, 'untertitel', true);
$class = wp_get_post_terms( $post_id, 'post_tag', $termsargs );
$bgimage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<section class="section zutaten '.$class[0].'" style="background-image: url('.$bgimage.')">';
echo '<article class="zutaten-beschreibung">';
echo '<header>';
echo '<h2><strong>'.$headline.'</strong></h2>';
echo '<h3>'.$subtitle.'</h3>';
echo '<div class="zutaten-beschreibung-content">';
echo $content;
echo '</div>';
echo '</header>';
echo '</article>';
echo '</section>';
endwhile;
} else {
echo __('Fehler!');
}
wp_reset_postdata();
?>
RESULT:
What is going wrong here?
The function the_content() is printing the data. you should use get_the_content() instead.
I can see what's going wrong here. The echo $content; line is printing improper HTML. I know this because you've said in your caption there are many <p> tags where they should not be; and you're not (visibly) printing any <p> tags in this block of code.
Investigate what this method is returning: $content = the_content(); That should do the trick.

Categories